from django.db import models
from backend.utils.models import BaseModel


# Create your models here.

class Article(BaseModel):
    article_title = models.CharField(max_length=255, verbose_name="文章标题")
    article_abstract = models.CharField(max_length=255, verbose_name="文章摘要")
    article_content = models.TextField(verbose_name="文章内容")
    article_tags = models.ManyToManyField('TagOfArticle', verbose_name='文章标签', related_name='tag_articles')

    def __str__(self):
        return self.article_title

    class Meta:
        db_table = 'tb_article'
        verbose_name = '文章'
        verbose_name_plural = verbose_name


class TagOfArticle(BaseModel):
    tag_name = models.CharField(max_length=255, verbose_name="文章标签")

    def __str__(self):
        return self.tag_name

    def related_article_num(self):
        return Article.objects.filter(article_tags=self).count()

    class Meta:
        db_table = 'tb_tag_of_article'
        verbose_name = '文章标签'
        verbose_name_plural = verbose_name
